package net.qiqbframework.event.dao.mpimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.qiqbframework.common.dto.PageResultDto;
import net.qiqbframework.eventsourcing.dao.DomainEventDao;
import net.qiqbframework.eventsourcing.dao.DomainEventPageCondition;
import net.qiqbframework.eventsourcing.dao.DomainEventPo;
import net.qiqbframework.mp.BasicEntityPoDaoMpImpl;
import net.qiqbframework.mp.MpUtil;

import java.util.List;

public class DomainEventDaoMpImpl extends BasicEntityPoDaoMpImpl<DomainEventMapper, DomainEventPo> implements DomainEventDao {


    @Override
    public List<DomainEventPo> listSameAggregateRootReadyById(String id) {
        final DomainEventPo domainEventPo = getPoById(id);
        /*if (domainEventPo == null || !domainEventPo.getNoticeStatus().equals(NoticeStatus.ready.name())) {
            return new ArrayList<>();
        }*/
        QueryWrapper<DomainEventPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                //.eq(DomainEventPo::getNoticeStatus, NoticeStatus.ready.name())
                //.eq(StringUtils.isNotEmpty(domainEventPo.getNamespaceName()), DomainEventPo::getNamespaceName, domainEventPo.getNamespaceName())
                //.eq(StringUtils.isNotEmpty(domainEventPo.getApplicationName()), DomainEventPo::getApplicationName, domainEventPo.getApplicationName())
                .eq(StringUtils.isNotEmpty(domainEventPo.getAggregateIdentifier()), DomainEventPo::getAggregateIdentifier, domainEventPo.getAggregateIdentifier())
                .eq(StringUtils.isNotEmpty(domainEventPo.getAggregateName()), DomainEventPo::getAggregateName, domainEventPo.getAggregateName())
                .orderByDesc(DomainEventPo::getCreateTime)
        ;
        return super.list(queryWrapper);
    }

    @Override
    public DomainEventPo getPoById(String id) {
        return super.getById(id);
    }

    @Override
    public PageResultDto<DomainEventPo> list4Page(DomainEventPageCondition condition) {
        QueryWrapper<DomainEventPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotEmpty(condition.getId()), DomainEventPo::getId, condition.getId())
                //.like(StringUtils.isNotEmpty(condition.getPayloadParam()), DomainEventPo::getPayloadParam, condition.getPayloadParam())
                .like(StringUtils.isNotEmpty(condition.getPayloadClassName()), DomainEventPo::getPayloadType, condition.getPayloadClassName())
                //.eq(StringUtils.isNotEmpty(condition.getStatus()), DomainEventPo::getNoticeStatus, condition.getStatus())
                .orderByDesc(DomainEventPo::getCreateTime)
        ;
        final Page<DomainEventPo> page = super.page(MpUtil.ofPage(condition), queryWrapper);
        return MpUtil.toResult(page);
    }
}
